﻿require.config({
	urlArgs: "version=20170407",
    paths: {
        'jquery': 'packages/jquery',
        'boottree': 'packages/user-treeview',
        'roletree': 'packages/role-treeview',
        
        'bootstrap': '../bootstrap/js/bootstrap.min'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'bootstrap'
        },
    	'boottree':{
    		deps:['jquery'],
    		exports: 'boottree'
    	},
    	'roletree':{
    		deps:['jquery'],
    		exports: 'roletree'
    	}
    }
});

require(['jquery', 'boottree','roletree','bootstrap'],function($){
	var perms_list={};
	var account_id=0;
	var account_name="";
	var timeout;
	var treeshow_bool = false;
	$.ajax({
		type : 'POST',
		url : '/users/getId',
		//data : jsonda,
	    cache: false,
		async:false,
		//contentType: "application/json; charset=utf-8",
		dataType : 'json',
		success : function(res) {
			if (res.status == 200) {
					account_id=res.data.id;
					account_name=res.data.name;
					document.getElementById("user-info").innerHTML="&nbsp"+res.data.name;
			} else
				alert("加载失败！");
		},
		error : function(xml, err, throwInfo) {
			alert(err);
		}
    });
	$.ajax({
		type : 'POST',
		url : '/users/getPermList',
		//data : jsonda,
	    cache: false,
		async:false,
		//contentType: "application/json; charset=utf-8",
		dataType : 'json',
		success : function(res) {
			if (res.status == 200) {
				perms_list=res.entries;
			} else
				alert("加载失败！");
		},
		error : function(xml, err, throwInfo) {
			alert(err);
		}
    });
	setPage();
	getTree();
	//selfresize();
	getPermTree();
	$('#user-info').mouseover(function(){
		document.getElementById("user-info").innerHTML="&nbsp"+"退出登录";
	});
	$('#user-info').mouseleave(function(){
		document.getElementById("user-info").innerHTML="&nbsp"+account_name;
	});
	$('#show-tree').click(function(event){
		var hidebg=document.getElementById('hidebg');
    	if(!treeshow_bool){
    		$('#left-bar')[0].style.display="block";
    		treeshow_bool=true;
    	  	hidebg.style.display="block";
    	}else{
    		$('#left-bar')[0].style.display="";
    		treeshow_bool=false;
    		hidebg.style.display="none";
    	}
    });
	$('#hidebg').click(function(event){
		$('#left-bar')[0].style.display="";
		treeshow_bool=false;
		hidebg.style.display="none";
	});
	$('#user-logout').click(function(){
		$.ajax({
			type : 'GET',
			url : '/logout',
			//data : jsonda,
		    cache: false,
			async:false,
			//contentType: "application/json; charset=utf-8",
			dataType : 'json',
			success : function(res) {
				if (res.status == 200) {
					//循环遍历json，并将其加入显示中
					//alert("退出登录成功！");
						window.location.href="/login.html";
				} else
					alert("退出登录失败！");
			},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
	    });	
	});
	
	function setPage(){
		var add_role = true;
		var edit_role = true;
		var delete_role = true;
		
		for(var i in perms_list){
			var perm_list = perms_list[i];
			if(perm_list.Ename=="roles：create"){
				add_role=true;
			}else if(perm_list.Ename=="roles：edit"){
				edit_role=true;
			}else if(perm_list.Ename=="roles：delete"){
				delete_role=true;
			}
		}
		if(add_role){
			$("#add-role")[0].style.display="";
		}
		if(edit_role){
			$("#edit-role")[0].style.display="";
		}
		if(delete_role){
			$("#delete-role")[0].style.display="";
		}
		
	}
	
	function getTree(){
		$('#treeview5').innerHTML="";
		
		var defaultData = [];
		if(!!perms_list){
			for(var i in perms_list){
				var perm_list = perms_list[i];
				if(perm_list.Ename=="admin：selfplib"){
					defaultData.push({
	                	 id:'filelist-h0',
	                	 text: '我的方案',
	                   });
				}
				if(perm_list.Ename=="admin：selfmlib"){
					defaultData.push({
                   		id:'filelist-f0',
		            	text: '我的素材',
	                 });
				}
				if(perm_list.Ename=="admin：mlib"){
					defaultData.push({
                   		id:'filelist-s0',
		            	text: '素材库',
		            	
	                 });
				}
				if(perm_list.Ename=="admin：plib"){
					defaultData.push({
		            	 id:'filelist-t0',
		            	 text: '案例库',
	                 });
				}
				if(perm_list.Ename=="admin：useradmin"){
					defaultData.push({
		                 id:'filelist-e0',
		                 text: '用户管理',
		             });
				}
				if(perm_list.Ename=="admin：roleadmin"){
					defaultData.push({
				         id:'filelist-0',
				         text: '角色管理',
				    });
				}
			}
			defaultData.push({
			      id:'filelist-w0',
			      text: '个人中心',
			 });
		}
		$('#treeview5').treeview({
			 color: "#41464B",
	         selectedBackColor:"#41464B",
	         expandIcon: 'glyphicon glyphicon-chevron-right',
	         collapseIcon: 'glyphicon glyphicon-chevron-down',
	         nodeIcon: 'glyphicon',
	         data:  defaultData
	    });				
	}
	function getPermTree(){
		
		$('#role-tree').innerHTML="";
		$('#edit-role-tree').innerHTML="";
		var defaultSPData=[];
		var defaultSMData=[];
		var defaultMData=[];
		var defaultPData=[];
		var defaultUData=[];
		var defaultRData=[];
		var defaultOData=[];
		var perms_list={};
		$.ajax({
			type : 'GET',
			url : '/perms/list',
		    cache: false,
			async:false,
			contentType: "application/json; charset=utf-8",
			dataType : 'json',
			success : function(res) {
				if (res.status == 200) {
					perms_list = res.entries;
				} else
					alert("加载失败！");
			},
			error : function(xml, err, throwInfo) {
				alert(err);
			}
	    });	
		if(!!perms_list){
			for(var i in perms_list){
				var perm_list = perms_list[i];
				
				if(perm_list.page=='selfplib'){
					var jsonTDate={
						'id':'filelist-'+perm_list.id,
						'text':perm_list.name,
					}
					defaultSPData.push(jsonTDate);
				}else if(perm_list.page=='selfmlib'){
					var jsonTDate={
						'id':'filelist-'+perm_list.id,
						'text':perm_list.name,
					}
					defaultSMData.push(jsonTDate);	
				}else if(perm_list.page=='mlib'){
					var jsonTDate={
						'id':'filelist-'+perm_list.id,
						'text':perm_list.name,
					}
					defaultMData.push(jsonTDate);	
				}else if(perm_list.page=='plib'){
					var jsonTDate={
						'id':'filelist-'+perm_list.id,
						'text':perm_list.name,
					}
					defaultPData.push(jsonTDate);	
				}else if(perm_list.page=='useradmin'){
					var jsonTDate={
						'id':'filelist-'+perm_list.id,
						'text':perm_list.name,
					}
					defaultUData.push(jsonTDate);	
				}else if(perm_list.page=='roleadmin'){
					var jsonTDate={
							'id':'filelist-'+perm_list.id,
							'text':perm_list.name,
					}
					defaultRData.push(jsonTDate);	
				}else if(perm_list.page=='others'){
					var jsonTDate={
							'id':'filelist-'+perm_list.id,
							'text':perm_list.name,
					}
					defaultOData.push(jsonTDate);	
				}
			}
		}
		var defaultData = [
			               {
			                	 id:'filelist-0',
			                	 text: '我的方案',
			                	 nodes: defaultSPData
			               },
			               {
			                	 id:'filelist-0',
			                	 text: '我的素材',
			                	 nodes: defaultSMData,
			               },
			               {
			                	 id:'filelist-0',
			                	 text: '素材库',
			                	 nodes: defaultMData,
			               },
			               {
			                	 id:'filelist-0',
			                	 text: '方案库',
			                	 nodes: defaultPData,
			               },
			               {
			                	 id:'filelist-0',
			                	 text: '用户管理',
			                	 nodes: defaultUData,
			               },
			               {
			                	 id:'filelist-0',
			                	 text: '角色管理',
			                	 nodes: defaultRData,
			               },
			               {
			                	 id:'filelist-0',
			                	 text: '其它',
			                	 nodes: defaultOData,
			               },
		                 ];
		$('#role-tree').roletreeview({
	         color: "#428bca",
	         rootable:false,
	         expandIcon: 'glyphicon glyphicon-chevron-right',
	         collapseIcon: 'glyphicon glyphicon-chevron-down',
	        // nodeIcon: 'glyphicon glyphicon-bookmark',
	         showCheckbox: true,
	         data:  defaultData,
	         
	    });
		$('#edit-role-tree').roletreeview({
	         color: "#428bca",
	         rootable:false,
	         expandIcon: 'glyphicon glyphicon-chevron-right',
	         collapseIcon: 'glyphicon glyphicon-chevron-down',
	        // nodeIcon: 'glyphicon glyphicon-bookmark',
	         showCheckbox: true,
	         data:  defaultData,
	         
	    });
	}
	$('#edit-role').click(function(){
		$('#edit-role-tree').roletreeview(true).clearChecked();
		var select_role = $('#edit-role-tree').roletreeview(true).getSelectedId();
		if(select_role==""){
			alert("未选中角色");
			return;
		}
		$('#edit-role-name').val($('#role-'+select_role+' span')[0].innerHTML);
		getRolePerms(select_role);
		
		$('#editRole').modal('show');
	});
	$('#delete-role').click(function(){
		
		var select_role = $('#edit-role-tree').roletreeview(true).getSelectedId();
		if(select_role==""){
			alert("未选中角色");
			return;
		}
		
		$('#deleteRole').modal('show');
	});
	
	var findCheckableNodess = function() {  
        return $checkableTree.roletreeview('search', [ $('#input-check-node').val(), { ignoreCase: false, exactMatch: false } ]);  
    };  
     
     
 	function getRolePerms(selected_roleid){
		if(!!selected_roleid){
			
			$('#role-name').val($('#role-'+selected_roleid+' span')[0].innerHTML);
			var perms_list={};
			$.ajax({
				type : 'GET',
				url : '/roles/'+selected_roleid+'/listbyId',
			    cache: false,
				async:false,
				contentType: "application/json; charset=utf-8",
				dataType : 'json',
				success : function(res) {
					if (res.status == 200) {
						perms_list = res.entries;
					} else
						alert("加载失败！");
				},
				error : function(xml, err, throwInfo) {
					alert(err);
				}
		    });	
			if(!!perms_list){
				for(var i in perms_list){
					var perm_list = perms_list[i];
					$('#edit-role-tree').roletreeview(true).setChecked(perm_list.id.toString());
				}
			}
		} 
	}
 	
 	
 	
    function selfresize(){
    	//if(window.location.pathname=='/admin/selfmlib.html'){

	  	$('#searchresultall').css('max-height',$(window).height()-$("#term-show").offset().top+'px');	
    	//alert($("#searchresultall").offset().top);
    	if($(window).width()<450){
  
    		$('#function-bar').css('top','0px');
    		$('#toolicon').css('margin-left','8px');
    		$('#user-message').css('margin-right','20px');

    		$('#left-bar').width('98%');
    		$('#left-bar').css('margin-top','30px');
    		$('#term-show').width('98%');
    		
    		if(window.location.pathname=='/user/selfplib.html'){
    			$('#term-show').css('margin-top','70px');
    			$('#toolicon').css('margin-left','8px');
    			$('#searchtext').css('width','90px');
    			$('#searchdiv').css('width','10px');
    			$('#searchbar').css('margin-right','30px');
    			$('#toolicon').css('font-size','8px');
    			$('#topic-mind').css('font-size','25px');
    			
    		}else{
    			$('#term-show').css('margin-top','9px');
    		}
    		
    		$('#term-show').css('min-height',$(window).height()-$("#term-show").offset().top+'px');
    		
    	
    	}else if($(window).width()<697){
    		//alert($(window).width());
    		$('#function-bar').css('top','10px');
    		$('#toolicon').css('margin-left','80px');
    		$('#user-message').css('margin-right','20px');
    		
    		$('#left-bar').width('98%');
    		$('#left-bar').css('margin-top','50px');
    		
    		$('#term-show').width('98%');
    		if(window.location.pathname=='/user/selfplib.html'){
    			$('#term-show').css('margin-top','80px');
    			$('#toolicon').css('margin-left','8px');
    			$('#searchtext').css('width','100px');
    			$('#searchdiv').css('width','10px');
    			$('#searchbar').css('margin-right','50px');
    			$('#topic-mind').css('font-size','35px');
    			
    		}else{
    			$('#term-show').css('margin-top','9px');
    		}
    		
    		$('#term-show').css('min-height',$(window).height()-$("#term-show").offset().top+'px');
    		
    		
    	}else{
    		$('#function-bar').css('top','30px');
    		$('#toolicon').css('margin-left','80px');
    		$('#user-message').css('margin-right','80px');
    		$('#left-bar').width('10%');
    		$('#left-bar').css('margin-top','130px');
    		
    		$('#term-show').width('83%');
    		if(window.location.pathname=='/user/selfplib.html'){
    			$('#searchdiv').css('width','100px');
    			$('#searchtext').css('width','200px');
    			$('#searchbar').css('margin-right','80px');
    			$('#topic-mind').css('font-size','35px');
    		}else{
    			
    		}
    		$('#term-show').css('margin-top','90px');
    		$('#term-show').css('min-height',$(window).height()-$("#term-show").offset().top-50+'px');
    	}
    	//}
    }
});
